package com.gnerv.author.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gnerv.author.dto.ArticleDTO;
import com.gnerv.author.entity.Article;
import com.gnerv.author.entity.ArticleConfig;
import com.gnerv.author.entity.ArticleStatistics;
import com.gnerv.author.entity.CatalogArticle;
import com.gnerv.author.entity.Content;
import com.gnerv.author.mapper.ArticleConfigMapper;
import com.gnerv.author.mapper.ArticleMapper;
import com.gnerv.author.mapper.ArticleStatisticsMapper;
import com.gnerv.author.mapper.CatalogArticleMapper;
import com.gnerv.author.mapper.ContentMapper;
import com.gnerv.author.service.IArticleService;
import com.gnerv.author.service.IArticleStatisticsService;
import com.gnerv.author.util.UUIDUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 博客平台-文章表 服务实现类
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-07-15
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    public static String baseArticleUrl = "/article/";

    @Autowired
    CatalogArticleMapper catalogArticleMapper;

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ArticleConfigMapper articleConfigMapper;

    @Autowired
    ContentMapper contentMapper;

    @Autowired
    ArticleStatisticsMapper articleStatisticsMapper;

    @Override
    public boolean createArticle(Article article) {
        return true;
    }

    @Override
    public boolean deleteArticle(String bId) {
        articleMapper.delete(new QueryWrapper<>(new Article(bId)));
        articleConfigMapper.delete(new QueryWrapper<>(new ArticleConfig(bId)));
        contentMapper.delete(new QueryWrapper<>(new Content(bId)));
        catalogArticleMapper.delete(new QueryWrapper<>(new CatalogArticle(bId)));
        return true;
    }

    @Override
    public boolean updateArticle(Article article) {
        return true;
    }

    @Override
    public ArticleDTO selectArticle(String bId) {
        ArticleDTO article = articleMapper.selectArticle(bId);
        return article;
    }

    @Override
    public List<ArticleDTO> listArticle(ArticleDTO articleDTO, Integer state) {
        List<ArticleDTO> articles = articleMapper.listArticle(articleDTO, state);
        return articles;
    }

    @Override
    public List<ArticleDTO> listArticleByCid(String cId, Integer state) {
        List<ArticleDTO> articles = articleMapper.listArticleByCid(cId, state);
        return articles;
    }

    @Override
    public boolean releaseArticle(String bId) {
        articleMapper.releaseArticle(bId);
        return true;
    }

    @Override
    public boolean unReleaseArticle(String bId) {
        articleMapper.unReleaseArticle(bId);
        return true;
    }

    @Override
    public boolean saveArticle(ArticleDTO articleDTO) {
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO, article);
        if (StringUtils.isEmpty(articleDTO.getbId())) {
            String aId = UUIDUtil.getLower();
            article.setbId(aId);
            articleMapper.insert(article);
            articleConfigMapper.insert(new ArticleConfig(aId, articleDTO.getCommentary()));
            contentMapper.insert(new Content(aId, articleDTO.getContent()));
            catalogArticleMapper.insert(new CatalogArticle(articleDTO.getcId(), aId));
            articleStatisticsMapper.insert(new ArticleStatistics(aId));
        } else {
            String aId = articleDTO.getbId();
            articleMapper.update(article, new QueryWrapper<>(new Article(aId)));
            articleConfigMapper.update(new ArticleConfig(aId, articleDTO.getCommentary()),
                    new QueryWrapper<>(new ArticleConfig(aId)));
            contentMapper.update(new Content(aId, articleDTO.getContent()), new QueryWrapper<>(new Content(aId)));
            catalogArticleMapper.update(new CatalogArticle(articleDTO.getcId(), aId),
                    new QueryWrapper<>(new CatalogArticle(aId)));
            articleStatisticsMapper.insert(new ArticleStatistics(aId));
        }
        return true;
    }
}
